package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.utils.PageBean;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemController")
public class ItemController {

    @DubboReference
    private ItemService itemService;

    //分页展示
    @RequestMapping("findAll")
    @ResponseBody
    public AjaxResult findAll(@RequestParam(defaultValue = "0") Long parentId,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String name ){

        try {
            PageBean<TbItemCat> pageBean = itemService.findAll(parentId,pageNum, pageSize, name);
            return AjaxResult.OK().setData(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取一级联动
    @RequestMapping("getOne")
    @ResponseBody
    public AjaxResult getOne(Long id){
        List<TbItemCat> list = itemService.getOne(id);
        return AjaxResult.OK().setData(list);
    }

    //增加分类
    @RequestMapping("addItem")
    @ResponseBody
    public AjaxResult addItem(TbItemCat itemCat){
        if (itemCat == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            itemService.addItem(itemCat);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显操作
    @RequestMapping("editItem")
    @ResponseBody
    public AjaxResult<TbItemCat> editItem(Long id, Model model){
        if (id == null && id == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbItemCat itemCat = itemService.editItem(id);
            model.addAttribute("item",itemCat);
            return AjaxResult.OK().setData(itemCat);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改操作
    @RequestMapping("updateItem")
    @ResponseBody
    public AjaxResult updateItem(TbItemCat itemCat){
        if (itemCat == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            itemService.updateItem(itemCat);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除方法
    @RequestMapping("deleteItem")
    @ResponseBody
    public AjaxResult deleteItem(Long [] ids){
        if (ids.length == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            itemService.deleteItem(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
